import { findSecThrdCtgysByFstCtgyId } from '../defmodel/OneToMany'
import { sequelize } from '../../BaseDao'
// import convert from '../../../tstypes/three'
import { convert } from '../modeltypes.ts'
import FirstCtgyModel from '../defmodel/FirstCtgy.ts'
class CtgyDao {
  static ctgyDao: CtgyDao = new CtgyDao()

  async findSecThrdCtgys(firstCtgyId: string) {
    const r = await findSecThrdCtgysByFstCtgyId(+firstCtgyId)
    return r
  }

  async findSecThrdCtgysBySql(firstCtgyId: string) {
    const sql = `SELECT * FROM secondctgy sc inner join thirdctgy tc on sc.secondCtgyId=tc.secctgyid WHERE sc.firstCtgyId=${firstCtgyId}`
    const r = await sequelize.query(sql)
    return r[0]
    // const r = await findSecThrdCtgysByFstCtgyId(+firstCtgyId)
    // return r
  }

  async findSecThrdCtgysByCon(firstCtgyId: string) {
    const sql = `SELECT * FROM secondctgy sc inner join thirdctgy tc on sc.secondCtgyId=tc.secctgyid WHERE sc.firstCtgyId=${firstCtgyId}`
    const r = await sequelize.query(sql)
    return convert(r[0] as any[])
    // const r = await findSecThrdCtgysByFstCtgyId(+firstCtgyId)
    // return r
  }

  async findFirstCtgys() {
    return FirstCtgyModel.findAll({
      raw: true
    })
  }
}

export const { ctgyDao } = CtgyDao
